package com.xinchen.aiodispose.vm

import com.blankj.utilcode.util.GsonUtils
import com.maple.model.data.AioUserInfo
import com.maple.model.entity.aio.LoginEntity
import com.maple.utils.manager.SingleLiveEvent
import com.maple.utils.store.AioDBHelper
import com.maple.utils.store.AppDBHelper
import com.maple.utils.util.HttpRequestUtils
import com.maple.utils.util.LogUtils
import com.xinchen.aiodispose.base.BaseViewModel
import com.xinchen.aiodispose.model.repository.CommonRepository
import java.util.WeakHashMap

class AccountViewModel: BaseViewModel(){

    private val repository by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { CommonRepository() }

    val loginEvent: SingleLiveEvent<Any> = SingleLiveEvent()

    fun onLogin(account: String,password: String,isRememberPwd: Boolean,platform: Int) {
        HttpRequestUtils.clearAccessToken()
        launchOnlyResult(block = {
            val params: WeakHashMap<String, Any> = WeakHashMap()
            params.put("userName", account)
            params.put("password", password)
            params.put("platform", platform.toString())
            repository.login(params)
        }, success = {
            it.let { data ->
                LogUtils.logGGQ("登录：${data}")
                val entity = GsonUtils.fromJson(GsonUtils.toJson(data), LoginEntity::class.java)

                entity?.let { user ->
                    saveUserInfo(account,password,isRememberPwd,platform,user)
                }?:let {
                    LogUtils.logGGQ("==获取数据为空！！！===>>>")
                    defUI.onToast("登录失败！")
                }
            }
        })
    }

    private fun saveUserInfo(account: String,password: String,isRememberPwd: Boolean,platform: Int,user: LoginEntity) {
        val userInfo: AioUserInfo = AioUserInfo()
    }
}